////////////////////////////////////////////////////////////////////////////////
//
//  The MIT License
//
//  Copyright (c) 2008 - 2009. Dimarik
//
//  Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
//
//  The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
//
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
//
////////////////////////////////////////////////////////////////////////////////

package tx.framework.service {

	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.utils.ByteArray;

	/**
	 *  @author				Dimarik
	 *  @version			1.0
	 *  @langversion		3.0
	 *  @playerversion		9.0
	 *
	 */
	public final class AMFHTTPService extends HTTPService {

		//---------------------------------------------------------------------
		//
		//   Constructor
		//
		//---------------------------------------------------------------------

		public function AMFHTTPService() {
			super();
		}

		//---------------------------------------------------------------------
		//
		//   Properties
		//
		//---------------------------------------------------------------------

		public var isAuthorized:		Boolean = false;

		public var VIEWER_ID:			String;

		//---------------------------------------------------------------------
		//
		//   Private variables
		//
		//---------------------------------------------------------------------

		//---------------------------------------------------------------------
		//
		//   Overriden public methods
		//
		//---------------------------------------------------------------------

		//-----------------------------
		//  Load data
		//-----------------------------
		public override function load(responder:Object, options:Object = null):void {
			if (!this.isAuthorized && options && options['object'] != 'auth') return;

			if (!options) {
				options = new Object();
				options.viewer_id = this.VIEWER_ID;
			}

			super.dispatchEvent( new Event(Event.OPEN) );

			super.load(responder, options);
		}

		//---------------------------------------------------------------------
		//
		//   Private methods
		//
		//---------------------------------------------------------------------

		//---------------------------------------------------------------------
		//
		//   Events handlers
		//
		//---------------------------------------------------------------------

		protected override function handler_success(event:Event):void {
			super.dispatchEvent( new Event(Event.COMPLETE) );

			var response:	Object;
			var data:		Object = (event.target as URLLoader).data;

			try {
				response = (data as ByteArray).readObject();
			} catch (error:Error) {
				super.handler_fault(event);
				return;
			}

			(data as ByteArray).position = 0;
			super.currentRequest.responder.result( response );
			super.currentRequest = null;
			super.requestNext();
		}

	}
}